const fs = require('fs')
const path = require('path')
const FormData = require('form-data')
const fetch = require('node-fetch')

// 配置上传信息
const config = {
  uploadUrl: process.env.UPLOAD_URL || 'https://example.com/upload',
  archivePath: path.resolve(__dirname, '../dist.zip'),
  projectToken: process.env.PROJECT_TOKEN || 'your-project-token'
}

async function uploadArchive() {
  try {
    // 检查文件是否存在
    if (!fs.existsSync(config.archivePath)) {
      console.error('压缩文件不存在，请先执行构建和压缩命令')
      process.exit(1)
    }

    // 创建表单数据
    const form = new FormData()
    form.append('file', fs.createReadStream(config.archivePath))
    form.append('token', config.projectToken)
    form.append('project', 'huarunhouhai')

    console.log('开始上传文件...')

    // 发送请求
    const response = await fetch(config.uploadUrl, {
      method: 'POST',
      body: form
    })

    if (!response.ok) {
      throw new Error(`上传失败: ${response.statusText}`)
    }

    const result = await response.json()
    console.log('上传成功:', result)
  } catch (error) {
    console.error('上传过程中出错:', error.message)
    process.exit(1)
  }
}

uploadArchive()